<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * Created by PhpStorm.
 * User: HP
 * Date: 2017/6/21
 * Time: 10:14
 */
class Goodstype extends Admin_Controller {
    public function __construct()
    {
        parent::__construct();
        $this->load->library('form_validation');
        $this->load->library('Pagination');
        $this->load->model('goodstype_model');
        $this->load->model('GoodsAttribute_model');
    }
   //增
    public function goodstype_add(){
        if ($_POST){
            $this->form_validation->set_rules('type_name', '商品类型名称', 'required');
            if ($this->form_validation->run() == FALSE){
                $data['message'] = validation_errors();
                $data['url'] = site_url('Admin/Goodstype/goodstype_add');
                $data['wait'] = 3;
                $this->load->view('message.html',$data);
            }else{
                $data['type_name'] = $this->input->post('type_name',true);
                if ($this->goodstype_model->add_goodstype($data)){
                    unset($data);
                    $data['message'] = "添加商品类型成功";
                    $data['url'] = site_url('Admin/Goodstype/goodstype_list');
                    $data['wait'] = 3;
                    $this->load->view('message.html',$data);
                }else{
                    unset($data);
                    $data['message'] = "添加商品类型失败";
                    $data['url'] = site_url('Admin/Goodstype/goodstype_add');
                    $data['wait'] = 3;
                    $this->load->view('message.html',$data);
                }
            }
        }else{
            $this->load->view('Goods/type_add.html');
        }
    }
    //删
    public function goodstype_delete(){
        $type_id = $this->uri->segment(4);
        if ($this->goodstype_model->delete_oneGoodstype($type_id)){
            $data['message'] = '删除成功';
        }else{
            $data['message'] = '删除失败';
        }
        $data['url'] = site_url('Admin/Goodstype/goodstype_list');
        $data['wait'] = 3;
        $this->load->view('message.html',$data);
    }
    //改
    public function goodstype_edit(){
        if ($_POST){
            $type_id = $this->input->post('type_id');
            $this->form_validation->set_rules('type_name', '商品类型名称', 'required');
            if ($this->form_validation->run() == FALSE){
                $data['message'] = validation_errors();
                $data['url'] = site_url('Admin/Goodstype/goodstype_edit/'.$type_id);
                $data['wait'] = 3;
                $this->load->view('message.html',$data);
            }else{
                $data['type_name'] = $this->input->post('type_name',true);
                if ($this->goodstype_model->find_name($data['type_name'],$type_id)){
                    unset($data);
                    $data['message'] = "商品类型已存在，请更改为其他类型";
                    $data['url'] = site_url('Admin/Goodstype/goodstype_edit/'.$type_id);
                    $data['wait'] = 3;
                    $this->load->view('message.html',$data);
                }else{
                    if ($this->goodstype_model->update_goodstype($data,$type_id)){
                        unset($data);
                        $data['message'] = "更改商品类型成功";
                        $data['url'] = site_url('Admin/Goodstype/goodstype_list');
                        $data['wait'] = 3;
                        $this->load->view('message.html',$data);
                    }else{
                        unset($data);
                        $data['message'] = "更改商品类型失败";
                        $data['url'] = site_url('Admin/Goodstype/goodstype_edit/'.$type_id);
                        $data['wait'] = 3;
                        $this->load->view('message.html',$data);
                    }
                }
            }
        }else{
            $type_id = $this->uri->segment(4);
            $data['type'] = $this->goodstype_model->one_goodstype($type_id);
            $this->load->view('Goods/type_edit.html',$data);
        }
    }
    //查
    public function goodstype_list($page = ''){
        $config['uri_segment'] = 4;
        $config['base_url'] = 'http://localhost/ci/index.php/Admin/Goodstype/goodstype_list';
        $config['total_rows'] = $this->goodstype_model->get_allNum();
        $config['per_page'] = 2;
        $config['next_link'] = '下一页';
        $config['prev_link'] = '上一页';
        $config['first_link'] = '首页';
        $config['last_link'] = '尾页';
        $this->pagination->initialize($config);
        $data['pages'] =  $this->pagination->create_links();
        $data['types'] = $this->goodstype_model->get_pageData($config['per_page'],$page);
        foreach($data['types'] as $k=>$type){
           $data['types'][$k]['attribute_num'] =  $this->GoodsAttribute_model->attribute_num($type['type_id']);
        }
        $this->load->view('Goods/type_list.html',$data);
    }

}